1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module adw.Toast;
26 
27 private import adw.c.functions;
28 public  import adw.c.types;
29 private import glib.ConstructionException;
30 private import glib.Str;
31 private import glib.Variant;
32 private import glib.c.functions;
33 private import gobject.ObjectG;
34 private import gobject.Signals;
35 private import std.algorithm;
36 
37 
38 /**
39  * A helper object for [class@ToastOverlay].
40  * 
41  * Toasts are meant to be passed into [method@ToastOverlay.add_toast] as
42  * follows:
43  * 
44  * ```c
45  * adw_toast_overlay_add_toast (overlay, adw_toast_new (_("Simple Toast"));
46  * ```
47  * 
48  * <picture>
49  * <source srcset="toast-simple-dark.png" media="(prefers-color-scheme: dark)">
50  * <img src="toast-simple.png" alt="toast-simple">
51  * </picture>
52  * 
53  * Toasts always have a close button. They emit the [signal@Toast::dismissed]
54  * signal when disappearing.
55  * 
56  * [property@Toast:timeout] determines how long the toast stays on screen, while
57  * [property@Toast:priority] determines how it behaves if another toast is
58  * already being displayed.
59  * 
60  * ## Actions
61  * 
62  * Toasts can have one button on them, with a label and an attached
63  * [iface@Gio.Action].
64  * 
65  * ```c
66  * AdwToast *toast = adw_toast_new (_("Toast with Action"));
67  * 
68  * adw_toast_set_button_label (toast, _("_Example"));
69  * adw_toast_set_action_name (toast, "win.example");
70  * 
71  * adw_toast_overlay_add_toast (overlay, toast);
72  * ```
73  * 
74  * <picture>
75  * <source srcset="toast-action-dark.png" media="(prefers-color-scheme: dark)">
76  * <img src="toast-action.png" alt="toast-action">
77  * </picture>
78  * 
79  * ## Modifying toasts
80  * 
81  * Toasts can be modified after they have been shown. For this, an `AdwToast`
82  * reference must be kept around while the toast is visible.
83  * 
84  * A common use case for this is using toasts as undo prompts that stack with
85  * each other, allowing to batch undo the last deleted items:
86  * 
87  * ```c
88  * 
89  * static void
90  * toast_undo_cb (GtkWidget  *sender,
91  * const char *action,
92  * GVariant   *param)
93  * {
94  * // Undo the deletion
95  * }
96  * 
97  * static void
98  * dismissed_cb (MyWindow *self)
99  * {
100  * self->undo_toast = NULL;
101  * 
102  * // Permanently delete the items
103  * }
104  * 
105  * static void
106  * delete_item (MyWindow *self,
107  * MyItem   *item)
108  * {
109  * g_autofree char *title = NULL;
110  * int n_items;
111  * 
112  * // Mark the item as waiting for deletion
113  * n_items = ... // The number of waiting items
114  * 
115  * if (!self->undo_toast) {
116  * title = g_strdup_printf (_("‘%s’ deleted"), ...);
117  * 
118  * self->undo_toast = adw_toast_new (title);
119  * 
120  * adw_toast_set_priority (self->undo_toast, ADW_TOAST_PRIORITY_HIGH);
121  * adw_toast_set_button_label (self->undo_toast, _("_Undo"));
122  * adw_toast_set_action_name (self->undo_toast, "toast.undo");
123  * 
124  * g_signal_connect_swapped (self->undo_toast, "dismissed",
125  * G_CALLBACK (dismissed_cb), self);
126  * 
127  * adw_toast_overlay_add_toast (self->toast_overlay, self->undo_toast);
128  * 
129  * return;
130  * }
131  * 
132  * title =
133  * g_strdup_printf (ngettext ("<span font_features='tnum=1'>%d</span> item deleted",
134  * "<span font_features='tnum=1'>%d</span> items deleted",
135  * n_items), n_items);
136  * 
137  * adw_toast_set_title (self->undo_toast, title);
138  * }
139  * 
140  * static void
141  * my_window_class_init (MyWindowClass *klass)
142  * {
143  * GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
144  * 
145  * gtk_widget_class_install_action (widget_class, "toast.undo", NULL, toast_undo_cb);
146  * }
147  * ```
148  * 
149  * <picture>
150  * <source srcset="toast-undo-dark.png" media="(prefers-color-scheme: dark)">
151  * <img src="toast-undo.png" alt="toast-undo">
152  * </picture>
153  *
154  * Since: 1.0
155  */
156 public class Toast : ObjectG
157 {
158 	/** the main Gtk struct */
159 	protected AdwToast* adwToast;
160 
161 	/** Get the main Gtk struct */
162 	public AdwToast* getToastStruct(bool transferOwnership = false)
163 	{
164 		if (transferOwnership)
165 			ownedRef = false;
166 		return adwToast;
167 	}
168 
169 	/** the main Gtk struct as a void* */
170 	protected override void* getStruct()
171 	{
172 		return cast(void*)adwToast;
173 	}
174 
175 	/**
176 	 * Sets our main struct and passes it to the parent class.
177 	 */
178 	public this (AdwToast* adwToast, bool ownedRef = false)
179 	{
180 		this.adwToast = adwToast;
181 		super(cast(GObject*)adwToast, ownedRef);
182 	}
183 
184 
185 	/** */
186 	public static GType getType()
187 	{
188 		return adw_toast_get_type();
189 	}
190 
191 	/**
192 	 * Creates a new `AdwToast`.
193 	 *
194 	 * The toast will use @title as its title.
195 	 *
196 	 * @title can be marked up with the Pango text markup language.
197 	 *
198 	 * Params:
199 	 *     title = the title to be displayed
200 	 *
201 	 * Returns: the new created `AdwToast`
202 	 *
203 	 * Since: 1.0
204 	 *
205 	 * Throws: ConstructionException GTK+ fails to create the object.
206 	 */
207 	public this(string title)
208 	{
209 		auto __p = adw_toast_new(Str.toStringz(title));
210 
211 		if(__p is null)
212 		{
213 			throw new ConstructionException("null returned by new");
214 		}
215 
216 		this(cast(AdwToast*) __p, true);
217 	}
218 
219 	/**
220 	 * Dismisses @self.
221 	 *
222 	 * Since: 1.0
223 	 */
224 	public void dismiss()
225 	{
226 		adw_toast_dismiss(adwToast);
227 	}
228 
229 	/**
230 	 * Gets the name of the associated action.
231 	 *
232 	 * Returns: the action name
233 	 *
234 	 * Since: 1.0
235 	 */
236 	public string getActionName()
237 	{
238 		return Str.toString(adw_toast_get_action_name(adwToast));
239 	}
240 
241 	/**
242 	 * Gets the parameter for action invocations.
243 	 *
244 	 * Returns: the action target
245 	 *
246 	 * Since: 1.0
247 	 */
248 	public Variant getActionTargetValue()
249 	{
250 		auto __p = adw_toast_get_action_target_value(adwToast);
251 
252 		if(__p is null)
253 		{
254 			return null;
255 		}
256 
257 		return new Variant(cast(GVariant*) __p);
258 	}
259 
260 	/**
261 	 * Gets the label to show on the button.
262 	 *
263 	 * Returns: the button label
264 	 *
265 	 * Since: 1.0
266 	 */
267 	public string getButtonLabel()
268 	{
269 		return Str.toString(adw_toast_get_button_label(adwToast));
270 	}
271 
272 	/**
273 	 * Gets priority for @self.
274 	 *
275 	 * Returns: the priority
276 	 *
277 	 * Since: 1.0
278 	 */
279 	public AdwToastPriority getPriority()
280 	{
281 		return adw_toast_get_priority(adwToast);
282 	}
283 
284 	/**
285 	 * Gets timeout for @self.
286 	 *
287 	 * Returns: the timeout
288 	 *
289 	 * Since: 1.0
290 	 */
291 	public uint getTimeout()
292 	{
293 		return adw_toast_get_timeout(adwToast);
294 	}
295 
296 	/**
297 	 * Gets the title that will be displayed on the toast.
298 	 *
299 	 * Returns: the title
300 	 *
301 	 * Since: 1.0
302 	 */
303 	public string getTitle()
304 	{
305 		return Str.toString(adw_toast_get_title(adwToast));
306 	}
307 
308 	/**
309 	 * Sets the name of the associated action.
310 	 *
311 	 * Params:
312 	 *     actionName = the action name
313 	 *
314 	 * Since: 1.0
315 	 */
316 	public void setActionName(string actionName)
317 	{
318 		adw_toast_set_action_name(adwToast, Str.toStringz(actionName));
319 	}
320 
321 	/**
322 	 * Sets the parameter for action invocations.
323 	 *
324 	 * If the @action_target variant has a floating reference this function
325 	 * will sink it.
326 	 *
327 	 * Params:
328 	 *     actionTarget = the action target
329 	 *
330 	 * Since: 1.0
331 	 */
332 	public void setActionTargetValue(Variant actionTarget)
333 	{
334 		adw_toast_set_action_target_value(adwToast, (actionTarget is null) ? null : actionTarget.getVariantStruct());
335 	}
336 
337 	/**
338 	 * Sets the label to show on the button.
339 	 *
340 	 * It set to `NULL`, the button won't be shown.
341 	 *
342 	 * Params:
343 	 *     buttonLabel = a button label
344 	 *
345 	 * Since: 1.0
346 	 */
347 	public void setButtonLabel(string buttonLabel)
348 	{
349 		adw_toast_set_button_label(adwToast, Str.toStringz(buttonLabel));
350 	}
351 
352 	/**
353 	 * Sets the action name and its parameter.
354 	 *
355 	 * @detailed_action_name is a string in the format accepted by
356 	 * [func@Gio.Action.parse_detailed_name].
357 	 *
358 	 * Params:
359 	 *     detailedActionName = the detailed action name
360 	 *
361 	 * Since: 1.0
362 	 */
363 	public void setDetailedActionName(string detailedActionName)
364 	{
365 		adw_toast_set_detailed_action_name(adwToast, Str.toStringz(detailedActionName));
366 	}
367 
368 	/**
369 	 * Sets priority for @self.
370 	 *
371 	 * Priority controls how the toast behaves when another toast is already
372 	 * being displayed.
373 	 *
374 	 * If @priority is `ADW_TOAST_PRIORITY_NORMAL`, the toast will be queued.
375 	 *
376 	 * If @priority is `ADW_TOAST_PRIORITY_HIGH`, the toast will be displayed immediately,
377 	 * pushing the previous toast into the queue instead.
378 	 *
379 	 * Params:
380 	 *     priority = the priority
381 	 *
382 	 * Since: 1.0
383 	 */
384 	public void setPriority(AdwToastPriority priority)
385 	{
386 		adw_toast_set_priority(adwToast, priority);
387 	}
388 
389 	/**
390 	 * Sets timeout for @self.
391 	 *
392 	 * If @timeout is 0, the toast is displayed indefinitely until manually
393 	 * dismissed.
394 	 *
395 	 * Toasts cannot disappear while being hovered, pressed (on touchscreen), or
396 	 * have keyboard focus inside them.
397 	 *
398 	 * Params:
399 	 *     timeout = the timeout
400 	 *
401 	 * Since: 1.0
402 	 */
403 	public void setTimeout(uint timeout)
404 	{
405 		adw_toast_set_timeout(adwToast, timeout);
406 	}
407 
408 	/**
409 	 * Sets the title that will be displayed on the toast.
410 	 *
411 	 * Params:
412 	 *     title = a title
413 	 *
414 	 * Since: 1.0
415 	 */
416 	public void setTitle(string title)
417 	{
418 		adw_toast_set_title(adwToast, Str.toStringz(title));
419 	}
420 
421 	/**
422 	 * Emitted when the toast has been dismissed.
423 	 *
424 	 * Since: 1.0
425 	 */
426 	gulong addOnDismissed(void delegate(Toast) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
427 	{
428 		return Signals.connect(this, "dismissed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
429 	}
430 }